package com.telecom.cms.actions;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.telecom.cms.util.Constant;
import com.telecom.cms.dao.GetConnect;

/**
 * @author yueyingnishang
 * 
 */
public class CollectionDelAction extends Action {

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.apache.struts.action.Action#execute(org.apache.struts.action.ActionMapping,
	 *      org.apache.struts.action.ActionForm,
	 *      javax.servlet.http.HttpServletRequest,
	 *      javax.servlet.http.HttpServletResponse)
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// TODO Auto-generated method stub
		String folderid = request.getParameter("parfolderid");// 文件夹的 id
		Connection conn = GetConnect.getCsConnection();
		if (conn != null) {
			conn.setAutoCommit(false);
		}
		// 查得删除点所有子节点的id
		Vector child = new Vector();
		ResultSet rs = null;
		int flag = 0;
		String c = folderid;
		try {
			do {
				int i = 0;

				while (i < child.size()) {
					c = c + "," + child.get(i);
					i++;
				}
				flag = child.size();
				String sql = "select * from " + Constant.SEG_USER_FOLDER_DEF
						+ " where PAR_FOLEDER_ID in ( " + c + " )";
				PreparedStatement ps = conn.prepareStatement(sql);
				rs = ps.executeQuery();
				child.removeAllElements();
				while (rs.next()) {
					String r = (new Integer(rs.getInt("FOLEDER_ID")))
							.toString();
					child.add(r);
				}
			} while (child.size() != flag);

			// 取出所有对应的app实例
			String sql = "select * from " + Constant.SEG_APP_SUBJECT_DEF
					+ " where FOLEDER_ID in( " + c + " )";
			;
			PreparedStatement ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			child.removeAllElements();
			while (rs.next()) {
				String r = (new Integer(rs.getInt("APP_SUBJECT_ID")))
						.toString();
				child.add(r);
			}

			int i = 1;
			String d = (String) child.get(0);
			while (i < child.size()) {
				d = d + "," + child.get(i);
				i++;
			}
			//删除实例
			sql="delete from " + Constant.SEG_APP_SUBJECT_DEF
			+ " where APP_SUBJECT_ID in( " + d + " )";
			ps = conn.prepareStatement(sql);
			ps.executeUpdate();
			//删除文件夹
			sql="delete from " + Constant.SEG_USER_FOLDER_DEF
			+ " where FOLEDER_ID in( " + c + " )";
			ps = conn.prepareStatement(sql);
			ps.executeUpdate();
		} catch (Exception e) {
			// TODO: handle exception
		}
		if (conn != null) {
			try {
				conn.commit();
				conn.setAutoCommit(true);
			} catch (Exception e) {
				conn.rollback();
				return mapping.findForward("failure");
			}
		}

		return mapping.findForward("success");
	}

}
